package cn.chencq.gdt.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.chencq.gdt.common.Constants;
import cn.chencq.gdt.vo.Mobile;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.limei.adplatform.redis.cluster.RedisClusterFactory;

public class MoblieInfoCache {

	private static final String IOS_MODEL = "Mozilla/5.0 (iPhone; CPU iPhone OS {os_ver} like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile Safari/601.1";
	private static final String ANDROID_MODEL = "Mozilla/5.0 (Linux; U; Android {os_ver}; zh-cn; {brand} {model} Build/{productLine}) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
	
	private static LoadingCache<String, List<Mobile>> mobileCache = 
			CacheBuilder.newBuilder()
			.expireAfterAccess(10, TimeUnit.MINUTES)
			.refreshAfterWrite(10, TimeUnit.MINUTES)
			.build(new CacheLoader<String, List<Mobile>>() {

				@Override
				public List<Mobile> load(String os) throws Exception {
					List<Mobile> ml = new ArrayList<Mobile>();
					String key = null;
					if(os.equals("ios")){
						key = Constants.IOS_USER_AGENT;
					}else{
						key = Constants.ANDROID_USER_AGENT;
					}
					
					Set<String> ss = RedisClusterFactory.smembers(Constants.CLUSTER, key);
					
					//brand|model|os_ver|w|h|dpi
					for (String s : ss) {
						try {
							String[] arr = s.split("\\|");
							for (int i = 0; i < arr.length; i++) {
								Mobile m = new Mobile();
								String brand = arr[0];
								m.setBrand(brand);
								
								String model = arr[1];
								m.setModel(model);
								
								m.setOs(os);
								
								String os_ver = arr[2];
								m.setOs_ver(arr[2]);
								
								m.setW(Integer.parseInt(arr[3]));
								m.setH(Integer.parseInt(arr[4]));
								m.setDpi(Integer.parseInt(arr[5]));
								
								String userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile Safari/601.1";
								if(os.equals("ios")){
									userAgent = IOS_MODEL.replace("{os_ver}", os_ver.replaceAll("\\.", "_"));
								}else if(os.equals("android")){
									String productLine = brand+model;
									userAgent = ANDROID_MODEL.replace("{os_ver}", os_ver).replace("{brand}", brand).replace("{model}", model).replace("{productLine}", productLine);
								}
								m.setUserAgent(userAgent);
								
								ml.add(m);
							}
						} catch (Exception ex) {
							ex.printStackTrace();
							continue;
						}
					}
					return ml;
				}

	});
	
	
	public static List<Mobile> getList(String os) {
		List<Mobile> mobile = new ArrayList<Mobile>();
		try {
			mobile = mobileCache.get(os);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mobile;
	}

	public static Mobile getOne(String os) {
		Mobile mobile = null;
		try {
			List<Mobile> list = getList(os);
			int total = list.size();
			if (total > 0) {
				int random = new Random().nextInt(total);
				mobile = list.get(random);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mobile;
	}
	
	
}
